package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;

public class SplashActivity extends AppCompatActivity {

    private Long mStartTime;
    private final long mDelayMs = 300;//splash显示的时间长度
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            long passTime = System.currentTimeMillis() - mStartTime;
            if(passTime > mDelayMs) {
                mHandler.post(gotoLoginActivity);
            } else {
                mHandler.postDelayed(gotoLoginActivity,  mDelayMs - passTime );
            }
        }
    };
    Runnable gotoLoginActivity = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this,  LoginActivity.class);
            startActivity(intent);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStartTime = System.currentTimeMillis();

        //TODO 数据初始化 耗时操作
        setContentView(R.layout.activity_splash);
        LottieAnimationView animationView = findViewById(R.id.animation);
        animationView.setAnimation(R.raw.adrock);
        animationView.playAnimation();
        animationView.setRepeatCount(LottieDrawable.INFINITE);
        animationView.setRepeatMode(LottieDrawable.RESTART);


        mHandler.sendEmptyMessage(0);
    }
}
